<template>
  <div class="p20 bg-white console h100">
    <div class="mt10 mb20">
      <el-button class="theme-btn" @click="handleAdd">新增账号</el-button>
      <span class="ml20">
        <i class="label">性质：</i>
        <el-select
          placeholder="性质"
          clearable
          v-model="propertyType"
          @change="hendleUpdate"
        >
          <el-option
            :key="item.number"
            :label="item.name"
            :value="item.number"
            v-for="item in typeList"
          ></el-option>
        </el-select>
      </span>
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
    </div>
    <AdminDialog
      :loading="loading"
      @close="hanleClose"
      @update="handleUpdate"
      :addDialog="addDialog"
      :typeList="typeList"
    ></AdminDialog>
    <el-table
      v-loading="loading"
      border
      stripe
      element-loading-text="拼命加载中"
      :data="tableData"
    >
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="type" label="性质"></el-table-column>
      <el-table-column prop="year" label="年代"></el-table-column>
      <el-table-column prop="userName" label="账号"></el-table-column>
      <el-table-column prop="phone" label="联系电话">
        <template slot-scope="scope">{{
          scope.row.phone | formatPhone
        }}</template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间"></el-table-column>
      <el-table-column prop="operation" label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDelete(scope.row.id)"
            >注销</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";
import { getAllAdmin, deleteAdmin } from "@/apis/admin";
import { getAllDictionary } from "@/apis/dictionary";
import AdminDialog from "@/components/admin_dialog";

export default {
  name: "User",
  data() {
    return {
      pageTotal: 0,
      tableData: [],
      addDialog: false,
      propertyType: "",
      typeList: [],
      placeholder: "输入名称/账号/搜索"
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.$store.dispatch("pagination/resetPageNum");
    this.getAdmins(this.pageSize, this.pageNum);
    this.getBuildTypeList();
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "queryStr", "loading"])
  },
  methods: {
    async getBuildTypeList() {
      const res = await getAllDictionary({ fieldName: "buildType" });
      this.typeList = res.rows;
    },
    handleCurrentChange(val) {
      this.val = val;
      this.getBuilding(this.pageSize, this.pageNum);
    },
    hendleUpdate() {
      this.getAdmins(this.pageSize, this.pageNum);
    },
    async getAdmins(pageSize, pageNum) {
      const { object } = await getAllAdmin({
        pageNum,
        pageSize,
        queryStr: this.queryStr,
        propertyType: this.propertyType
      });

      this.pageTotal = object.total;
      this.tableData = object.records;
      this.$store.dispatch("load/setLoading", false);
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
    },
    handleUpdate() {
      this.getAdmins(this.pageSize, this.pageNum);
    },
    hanleClose() {
      this.addDialog = false;
      this.$store.dispatch("load/setLoading", false);
    },
    handleDelete(id) {
      this.$confirm("此操作将注销该账号, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // 
          // deleteAdmin({ propertyId: id }).then(res => {
          //   if (res && res.code === "200") {
          //     this.$notify({
          //       title: res.msg,
          //       type: "success"
          //     });
          //     this.$store.dispatch("load/setLoading", false);
          //     this.handleUpdate(this.pageSize, this.pageNum);
          //   }
          // });
          this.$notify({
            title: '操作成功！',
            type: "success"
          });
          this.$store.dispatch("load/setLoading", false);
         })
        .catch(() => {
          this.$message("已取消注销");
        });
    }
  },
  components: {
    Search,
    Pagination,
    AdminDialog
  }
};
</script>

<style scoped lang="less"></style>
